package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回充值产品信息
 */
public class RoleData_RechargeProductReturnProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 11003; }

    public Integer RechargeProductCount; //充值产品数量
    public ArrayList<RechargeProductItem> CurrItemList; //充值产品项

    /**
     * 充值产品项
     */
    public static class RechargeProductItem
    {
        public Integer RechargeProductId; //充值产品编号
        public String ProductDesc; //描述
        public byte CanBuy; //是否可以购买
        public Integer RemainDay; //剩余天数
        public byte DoubleFlag; //是否双倍
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(RechargeProductCount);
            for (int i = 0; i < RechargeProductCount; i++) {
                dataStream.WriteInt(CurrItemList.get(i).RechargeProductId);
                dataStream.WriteUTF8String(CurrItemList.get(i).ProductDesc);
                dataStream.WriteByte(CurrItemList.get(i).CanBuy);
                dataStream.WriteInt(CurrItemList.get(i).RemainDay);
                dataStream.WriteByte(CurrItemList.get(i).DoubleFlag);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static RoleData_RechargeProductReturnProto GetProto(byte[] buffer) {
        RoleData_RechargeProductReturnProto proto = new RoleData_RechargeProductReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.RechargeProductCount = dataStream.ReadInt();
            proto.CurrItemList = new ArrayList<RechargeProductItem>();
            for (int i = 0; i < proto.RechargeProductCount; i++) {
                RechargeProductItem _CurrItem = new RechargeProductItem();
                _CurrItem.RechargeProductId = dataStream.ReadInt();
                _CurrItem.ProductDesc = dataStream.ReadUTF8String();
                _CurrItem.CanBuy = (byte)dataStream.ReadByte();
                _CurrItem.RemainDay = dataStream.ReadInt();
                _CurrItem.DoubleFlag = (byte)dataStream.ReadByte();
                proto.CurrItemList.add(_CurrItem);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}